package com.ceba.test.modules.admin.dao;


import abc.ID;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import com.ceba.base.web.dao.IBaseDao;

public class MessageDao extends IBaseDao {
	/**
	 * 添加一个消息提示
	 * @param relationId 关联ID
	 * @param title 标题
	 * @param body 内容
	 * @param toUserId 提示用户
	 * @param url 内容链接地址
	 * @param type 消息类型：1代表流程审批
	 * @return
	 */
	public Boolean add(String relationId,String title,String body,
			String toUserId,String url,String type) throws Exception{
		IStore store = StoreManager.create("msg_message");
		IObject object = new XObject("msg_message");
		object.put("ID",ID.getGuid());
		object.put("RELATION_ID",relationId);
		object.put("TITLE",title);
		object.put("BODY",body);
		object.put("USER_ID",toUserId);
		object.put("IS_READ","0");
		object.put("URL",url);
		object.put("TYPE",type);
		object.setCreateTime();
		object.setCreateUser();
		
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}
	
	/**
	 * 删除一条数据
	 * @param id 数据id
	 * @return
	 */
	public Boolean delete(String id) throws Exception{
		IStore store = StoreManager.create("msg_message");

		IObject object = new XObject("msg_message");
		object.put("ID",id);
		
		
		if(store.remove(object)>0){
			return true;	
		}
		return false;
	}
	
	/**
	 * 获取某一个用户的消息列表
	 * @param toUserId 用户的对象
	 * @return
	 */
	public DataTable getUserList(String toUserId) throws Exception{
		
		Sql sql = new Sql("SELECT * FROM msg_message WHERE USER_ID=? AND IFNULL(IS_READ,'0')=0 ORDER BY CREATE_TIME DESC");
		sql.addParameter(toUserId);
		DataTable dt = SqlSessionManager.getDataTable(sql);
		dt.setTotal(dt.getRowCount());
		return dt;
	}
	
	/**
	 * 已读
	 * @param id 数据id
	 * @return
	 */
	public Boolean read(String id) throws Exception{
		IStore store = StoreManager.create("msg_message");

		IObject object = new XObject("msg_message");
		object.put("ID",id);
		object.put("IS_READ","1");
		
		if(store.update(object)>0){
			return true;	
		}
		return false;
	}
	
	/**
	 * 已读
	 * @param userId 用户id
	 * @param relationId 关联id
	 * @return
	 */
	public Boolean read(String toUserId,String relationId) throws Exception{
	
		
		Sql sql=new Sql("UPDATE msg_message SET IS_READ='1' WHERE USER_ID=? AND RELATION_ID=?");
		sql.addParameter("USER_ID", toUserId);
		sql.addParameter("RELATION_ID", relationId);
		
		
		if(SqlSessionManager.execute(sql)){
			return true;	
		}
		return false;
	}
	
	/**
	 * 全部已读
	 * @param toUserId 用户id
	 * @return
	 */
	public Boolean readAll(String toUserId) throws Exception{
	
		Sql sql=new Sql("UPDATE msg_message SET IS_READ='1' WHERE USER_ID=?");
		sql.addParameter("USER_ID", toUserId);
		
		
		if(SqlSessionManager.execute(sql)){
			delAllIsRead();
			return true;	
		}
		return false;
	}
	
	/**
	 * 删除所有已读
	 * @return
	 */
	public Boolean delAllIsRead()throws Exception{
		
		
		Sql sql=new Sql("DELETE FROM msg_message WHERE IS_READ='1'");
		
		if(SqlSessionManager.execute(sql)){
			return true;	
		}
		return false;
	}
}
